/*
 * Copyright 2019-2021 the langrundata.
 */
package com.caishi.lkx.user.model.re;

import cn.hutool.core.net.Ipv4Util;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.googlecode.ipv6.IPv6Address;
import com.zzw.common.entity.impl.BSIntEntity;
import com.caishi.lkx.common.ienum.IpType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;


@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_group_ip_re")
public class SysGroupIpReModel extends BSIntEntity<SysGroupIpReModel> {

    @Schema(title = "机构id")
    private Long groupId;

    @Schema(title = "IP类型")
    private IpType type;

    @Schema(title = "ip段")
    private String ip;

    private Integer omask;

    private byte[] val;

    private byte[] min;

    private byte[] max;

    @Schema(title = "备注")
    private String remark;

    @Schema(title = "绑定时间")
    private LocalDateTime bindTime;

    @Schema(title = "ip过期有效期")
    private LocalDate endTime;

    @Schema(title = "绑定用户")
    private String bindUserId;

    @Schema(title = "机构有效期")
    @TableField(exist = false)
    private LocalDate expireTime;
    @Schema(title = "考研机构有效期")
    @TableField(exist = false)
    private LocalDate kaoyanExpireTime;

    @Schema(title = "绑定用户")
    @TableField(exist = false)
    private String bindUserName;

    @Schema(title = "绑定用户")
    @TableField(exist = false)
    private String groupName;

    @Schema(title = "掩码位数")
    public int getMask() {
        return type == IpType.ipv4 ? 32 - omask : 128 - omask;
    }


    public void setValBigInteger(BigInteger intVal) {
        this.val = bigInter2Bytes(intVal);
    }


    public void setMinVal(BigInteger intVal) {
        this.min = bigInter2Bytes(intVal);
    }


    public void setMaxVal(BigInteger intVal) {
        this.max = bigInter2Bytes(intVal);
    }

    public static byte[] bigInter2Bytes(BigInteger intVal) {
        var bytes = intVal.toByteArray();
        var val = new byte[16];
        var len = bytes.length < 16 ? 4 : 16;
        System.arraycopy(bytes, (bytes.length & 3) == 0 ? 0 : 1, val, 16 - len, len);
        return val;
    }

    public String getStartIp() {
        if (min == null) return null;
        if (type == IpType.ipv4) {
            return Ipv4Util.longToIpv4(new BigInteger(min).longValue());
        } else {
            return IPv6Address.fromBigInteger(new BigInteger(min)).toString();
        }
    }

    public String getEndIp() {
        if (max == null) return null;
        if (type == IpType.ipv4) {
            return Ipv4Util.longToIpv4(new BigInteger(max).longValue());
        } else {
            return IPv6Address.fromBigInteger(new BigInteger(max)).toString();
        }
    }
    public String getWd() {
        if (val == null) return null;
        if (type == IpType.ipv4) {
            return Ipv4Util.longToIpv4(new BigInteger(val).longValue());
        } else {
            return IPv6Address.fromBigInteger(new BigInteger(val)).toString();
        }
    }
}
